
 /**  
 * Project Name:zhonghecrm  
 * File Name:SysRoleController.java  
 * Package Name:com.crm.controller  
 * Date:2016年10月26日下午2:19:38   
 *  
*/  
  
package com.crm.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.crm.model.SysMenu;
import com.crm.model.SysRole;
import com.crm.service.SysMenuService;
import com.crm.service.SysRoleService;
import com.crm.util.ResponseUtil;
import com.crm.util.ValidateUtil;
import com.github.pagehelper.Page;

/**  
 * ClassName:SysRoleController <br/>  
 * Function: TODO ADD FUNCTION. <br/>  
 * Reason:   TODO ADD REASON. <br/>  
 * Date:     2016年10月26日 下午2:19:38 <br/>  
 * @author   hanlinxiu  
 * @version    
 * @since    JDK 1.8  
 * @see        
 */
@Controller
@RequestMapping("/role")
public class SysRoleController {
	
	@Autowired
	private SysRoleService sysRoleService;
	
	@Autowired
	private SysMenuService sysMenuService;
	
	@RequestMapping("/index")
	public String index(Page<SysRole> page, ModelMap map){
		
		Page<SysRole> list = (Page<SysRole>) sysRoleService.selectAll(page);
		
		map.put("list", list);
		
		return "";
	}
	
	@RequestMapping("/getAll")
	@ResponseBody
	public Map<String,Object> getAll(HttpServletResponse response,HttpServletRequest request, Page<SysRole> page, SysRole t){
		
		Map<String, Object> map = new HashMap<>();
		
		Page<SysRole> list = (Page<SysRole>) sysRoleService.selectAll(page, t);
		
		map.put("list", list);
		map.put("status", "success");
		map.put("msg", "获取成功！");
		
		return map;
	}
	
	@RequestMapping("/load")
	@ResponseBody
	public void load(HttpServletResponse response,HttpServletRequest request, Page<SysRole> page, SysRole t){
		
		Page<SysRole> list = (Page<SysRole>) sysRoleService.selectAll(page, t);
		
		ResponseUtil.resultPage(response,request,list);
	}
	
	@RequestMapping("/getMenusById")
	@ResponseBody
	public Map<String,Object> getMenusById(String id){
		
		Map<String, Object> map = new HashMap<>();
		
		SysRole record = sysRoleService.selectByPrimaryKey(id);
		
		if(record.getMenus() != null)
			map.put("permisions", record.getMenus());
		
		map.put("status", "success");
		map.put("msg", "获取成功！");
		
		return map;
	}
	
	@RequestMapping("/addNewRecord")
	@ResponseBody
	public Map<String, Object> addNewRecord(SysRole record){
		
		Map<String, Object> map = new HashMap<>();
		
		if(ValidateUtil.isEmpty(record.getFullName()))
			throw new RuntimeException("名称不能为空！");
		
		sysRoleService.insert(record);
		
		map.put("status", "success");
		map.put("msg", "新增成功！");
		
		return map;
	}
	
	@RequestMapping("/deleteRecord")
	@ResponseBody
	public Map<String, Object> deleteRecord(SysRole record){
		
		Map<String, Object> map = new HashMap<>();
		
		record.setDeleteMark(0);
		sysRoleService.updateByPrimaryKey(record);
		
		map.put("status", "success");
		map.put("msg", "删除成功！");
		
		return map;
	}
	
	@RequestMapping("/updateRecord")
	@ResponseBody
	public Map<String, Object> updateRecord(SysRole record){
		
		Map<String, Object> map = new HashMap<>();
		
		sysRoleService.updateByPrimaryKey(record);
		
		map.put("status", "success");
		map.put("msg", "修改成功！");
		
		return map;
	}
}
  
